package com.base.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
 * 创建：使用Collections 集合工具类的静态方法unmodified~
 * 使用场景： 当你不希望别人修改你集合容器中的元素时。return  Collections.unmodifiableList(list)
 *
 * @author: May
 * @date 2021/3/5
 */
public class TestUnmodifiedCollection {
    public static void main(String[] args) {
        createUnmodifiedList();
    }

    public static void createUnmodifiedList() {
        List<String> stringList = new ArrayList<>();
        stringList.add("a");
        stringList.add("b");
        stringList.add("c");
        stringList.add("d");
        stringList.add("e");
        List<String> unmodifiableList = Collections.unmodifiableList(stringList);
        System.out.println("unmodifiableList = " + unmodifiableList);

        // update add
        unmodifiableList.add("f");
        System.out.println("unmodifiableList = " + unmodifiableList);
    }
}
